package cn.zyy.hotel.config;

import cn.zyy.hotel.realm.EmployeeRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig  {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/jsp/admin/login.jsp");
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("/assets/**","anon");
        map.put("/echarts/**","anon");
        map.put("/front/**","anon");
        map.put("/layui/**","anon");
        map.put("/layui_ext/**","anon");
        map.put("/imgs/**","anon");
        map.put("/home.jsp","anon");
        map.put("/index.jsp","anon");
        map.put("/login.jsp","anon");
        map.put("/register.jsp","anon");
        map.put("/userinfo.jsp","anon");
        map.put("/userOrder.jsp","anon");
        map.put("/jsp/detail.jsp","anon");
        map.put("/jsp/hotelList.jsp","anon");
        map.put("/index.html","anon");
        map.put("/orders/**","anon");
        map.put("/room/**","anon");
        map.put("/user/**","anon");
        map.put("/admin/employee/login","anon");
        map.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm employeeRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(employeeRealm);
        return defaultWebSecurityManager;
    }

    @Bean
    public EmployeeRealm employeeRealm(){
        EmployeeRealm employeeRealm = new EmployeeRealm();
        HashedCredentialsMatcher md5 = new HashedCredentialsMatcher("MD5");
        md5.setHashIterations(1024);
        employeeRealm.setCredentialsMatcher(md5);
        return employeeRealm;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
